package com.xishu.dao.jpa;

import com.xishu.entity.order.OrderDiscountTicketMapping;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.Tuple;
import java.util.List;

public interface OrderDiscountTicketMappingDao extends JpaRepository<OrderDiscountTicketMapping, Long>, JpaSpecificationExecutor<OrderDiscountTicketMapping> {
    /**
     * 年月日选项，不带名称查总计
     * @param startTime
     * @param endTime
     * @param timeUnit
     * @return
     */
    @Query(value = "select  FROM_UNIXTIME(create_time / 1000, :timeUnit) as x , name as y, sum(cash) as value " +
            "from order_discount_ticket_mapping m " +
            "where m.create_time >= :startTime and m.create_time <= :endTime group by name, FROM_UNIXTIME(create_time / 1000, :timeUnit)", nativeQuery = true)
    public List<Tuple> report(Long startTime, Long endTime, String timeUnit);

    /**
     * 年月日，带名称查总计
     * @param startTime
     * @param endTime
     * @param timeUnit
     * @param idTypeList
     * @return
     */
    @Query(value = "select  FROM_UNIXTIME(create_time / 1000, :timeUnit) as x , name as y, sum(cash) as value " +
            "from order_discount_ticket_mapping m " +
            "where m.create_time >= :startTime and m.create_time <= :endTime and name in (:idTypeList)" +
            " group by name, FROM_UNIXTIME(create_time / 1000, :timeUnit)", nativeQuery = true)
    public List<Tuple> idReport(Long startTime, Long endTime, String timeUnit, List<String> idTypeList);

    /**
     * 不带名称查总计
     * @param startTime
     * @param endTime
     * @return
     */
    @Query(value = "select name as y, sum(cash) as value " +
            "from order_discount_ticket_mapping m " +
            "where m.create_time >= :startTime and m.create_time <= :endTime group by name", nativeQuery = true)
    List<Tuple> reportTotal(Long startTime, Long endTime);

    /**
     * 带名称查总计
     * @param startTime
     * @param endTime
     * @param idTypeList
     * @return
     */
    @Query(value = "select name as y,sum(cash) as value " +
            "from order_discount_ticket_mapping m " +
            "where m.create_time >= :startTime and m.create_time <= :endTime and name in (:idTypeList)" +
            " group by name", nativeQuery = true)
    List<Tuple> idReportTotal(Long startTime, Long endTime, List<String> idTypeList);

}
